﻿using System;
using System.Reflection;
using Fabrication.Tests.TestClasses;
using NUnit.Framework;

namespace Fabrication.Tests
{
    [TestFixture]
    public class property_changed
    {
        [Test]
        public void should_not_remain_subscribes_to_property_changed_events()
        {
            var builtItem = new FabricationScope().Setup<NotifyPropertyChangedClass>()
                .Generate();

            var type = builtItem.GetType();

            var fia = type.GetFields(BindingFlags.Static | BindingFlags.Instance | BindingFlags.NonPublic);
            foreach (var fieldInfo in fia)
            {
                if (fieldInfo.Name == "PropertyChanged")
                {
                    var d = (Delegate)fieldInfo.GetValue(builtItem);
                    Assert.That(d, Is.Null);
                }
            }
        }

        [Test]
        public void should_not_fail_when_generating_classes_with_badly_written_property_changed_events()
        {
            var builtItem = new FabricationScope().Setup<NotifyPropertyChangedClass>()
                .Generate();

            var handlerRun = false;
            builtItem.PropertyChanged += (s, e) => handlerRun = true;

            builtItem.Name = "NewName";

            Assert.That(handlerRun, Is.True);
        }
    }
}

